var MouseMenu={
    //对象名
    name:"",
    //按键
    button:"",
    //选项
    options:[],
    //配置
    config:{
        //全局禁用浏览器右键
        disable_right_of_browser:false,
        //点击option后即关闭options 区
        close_after_click:true,
        //options 区 tag
        options_tag:'div',
        //options 区 id
        options_id:'mouse_menu',
        //options 区 class 用于css 修改时需要修改css文件
        options_class:'mouse_menu_options_class',
        //options 区 右偏移量 px
        options_right_offset:10,
        //options 区 下偏移量 px
        options_down_offset:5,
        //option tag
        option_tag:'li',
        //option 区 class 用于css 修改时需要修改css文件
        option_class:'mouse_menu_option_class',
        //shade 区 tag
        shade_tag:'div',
        //shade 区 id
        shade_id:'mouse_menu_shade',
        //shade 区 class 用于css 修改时需要修改css文件
        shade_class:'mouse_menu_shade_class'
    },
    //创建菜单
    create:function(name,button,options)
    {
        this.name=name;
        this.button=button;
        this.options=options;
    },
    //函数中调用
    check:function()
    {
        if(this.button==='all')
        {
            this.createOption();
        }else if(this.button==='left'&&event.which===1)
        {
            this.createOption();
        }else if(this.button==='right'&&event.which===3)
        {
            document.oncontextmenu=function()
            {
                return false;
            };
            this.createOption();
        }
        else if(this.button==='middle'&&event.which===2)
        {
            this.createOption();
        }
        else
        {
            return null;
        }
    },
    //创建菜单
    createOption:function()
    {
        let menu_name=this.name;
        let options_id=this.config.options_id;
        let option_tag=this.config.option_tag;
        let option_class=this.config.option_class;

        //创建shade(必须先创建shade)
        this.createShade();
        //创建options框
        this.createOptionsArea();

        for(let i in this.options)
        {
            let name=this.options[i].name;
            let id=this.options[i].id;
            var close_after_click=this.options[i].close_after_click;
            if(typeof close_after_click==="undefined")
            {
                close_after_click=this.config.close_after_click;
            }
            let option='';
            if(close_after_click)
            {
                option='<'+option_tag+' id="'+id+'" class="'+option_class+'" onclick="'+menu_name+'.options['+i+'].action();'+menu_name+'.remove()">'+name+'</'+option_tag+'>';
            }else
            {
                option='<'+option_tag+' id="'+id+'" class="'+option_class+'" onclick="'+menu_name+'.options['+i+'].action()">'+name+'</'+option_tag+'>';
            }
            $('#'+options_id).append(option);
        }
    },
    //检测禁用全局浏览器右键
    checkDisableRightOfBrowser:function()
    {
        if(this.config.disable_right_of_browser===false)
        {
            document.oncontextmenu=function()
            {
                return true;
            }
        }
    },
    //创建options框
    createOptionsArea:function()
    {
        let options_id=this.config.options_id;
        let options_tag=this.config.options_tag;
        let options_class=this.config.options_class;
        let options_right_offset=this.config.options_right_offset;
        let options_down_offset=this.config.options_down_offset;
        let top=event.pageY+options_down_offset;
        let left=event.pageX+options_right_offset;
        let style='style="display:block;top:'+top+'px;left:'+left+'px"';
        let div='<'+options_tag+' id="'+options_id+'" class="'+options_class+'" '+style+'></'+options_tag+'>';
        $('body').append(div);
    },
    //创建shade
    createShade:function()
    {
        let menu_name=this.name;
        let shade_tag=this.config.shade_tag;
        let shade_id=this.config.shade_id;
        let shade_class=this.config.shade_class;
        let shade='<'+shade_tag+' id="'+shade_id+'" class="'+shade_class+'" onmousedown="'+menu_name+'.remove();'+menu_name+'.checkDisableRightOfBrowser()"></'+shade_tag+'>';
        $('body').append(shade);
    },
    //清除
    remove:function()
    {
        this.removeOptions();
        this.removeShade();
        //检测禁用全局浏览器右键
        this.checkDisableRightOfBrowser();
    },
    //清除option框
    removeOptions:function()
    {
        let options_id=this.config.options_id;
        let options_tag=this.config.options_tag;
        $(options_tag+'#'+options_id).remove();
    },
    //清除shade
    removeShade:function()
    {
        let shade_tag=this.config.shade_tag;
        let shade_id=this.config.shade_id;
        $(shade_tag+'#'+shade_id).remove();
    }

};

//是否全局禁用浏览器右键
$(document).ready(function()
{
    if(MouseMenu.config.disable_right_of_browser===true)
    {
        document.oncontextmenu=function()
        {
            return false;
        }
    }
});

//函数参数不可删除
function createMouseMenu(instance_name,button,options)
{
    eval(instance_name+'=Object.create(MouseMenu)');
    eval(instance_name+'.create(instance_name,button,options)');
    eval(instance_name+'.check()');
}